<%


sMoreThanOne			= Request.Form("chkData")
sEmailTo				= Request.Form("lstEmail")
session("sEmailTo")		= Request.Form("lstEmail")
sMoreThanOneHidden		= Request.Form("chkDataHidden")
nIncomeTaxReferenceNo	= Request.Form("txtITN")
sNoTaxRefNoRsn			= Request.Form("lstTaxReason")
sNoTaxRefNoText			= Request.Form("txtOtherRsn")
nRSAID					= Request.Form("txtRSAId")
sOtherID				= Request.Form("txtOtherId")
sSurname				= Request.Form("txtSur")
sFirstNames				= Request.Form("txtName")
sInitials				= Request.Form("txtInit")
dtBirthDateFull			= Request.Form("txtBDFullDate")
dtBirthDateYear			= Request.Form("txtBDYear")
dtBirthDateMonth		= Request.Form("txtBDMonth")
dtBirthDateDay			= Request.Form("txtBDDay")
dtBirthDate				= Request.Form("txtBDFullDate")
sHome1					= Request.Form("txtHome1")
sHome2					= Request.Form("txtHome2")
sHome3					= Request.Form("txtHome3")
sHome4					= Request.Form("txtHome4")
sHomeCode				= Request.Form("txtHomeCode")
sChkHome				= Request.Form("chkHome")
sPost1					= Request.Form("txtPos1")
sPost2					= Request.Form("txtPos2")
sPost3					= Request.Form("txtPos3")
sPost4					= Request.Form("txtPos4")
sPostCode				= Request.Form("txtPosCode")
sFormType				= Request.Form("lstFormTypes")
nFundNumberPart1		= Request.Form("fund1")
nFundNumberPart2		= Request.Form("fund2")
nFundNumberPart3		= Request.Form("fund3")
nFundNumberValue		= Request.Form("FundNumberValue")
nFundNumber				= Request.Form("txtFundNumber")
nFundID					= Request.Form("numFundID")
nPAYERefNr				= Request.Form("txtEmplNr")

nEmpID					= Request.Form("numEmpID")

sFundName				= Request.Form("sFundName")
sFundType				= Request.Form("sFundType")
sEmpName				= Request.Form("sEmpName")

session("nTaxReasonIndex")	= Request.Form("txtlst")
session("nFormTypeIndex")	= Request.Form("txtFormTypeIndex")
sUser					= Request.Form("txtUsr")
sEmailSelectedIndex		= Request.Form("txtEmail")
sCheck					= Request.Form("txtCheck")
sFocusSet				= Request.Form("focusset")
sHomeChk				= Request.Form("txtHomechk")
lstEmail				= Request.Form("lstEmail")

nComID					= session("nComID")
nUserID					= session("nUserID")


Dim Home
Dim chkDateofBirth	

'if the "Copy from Home Address checkbox is checked
dim sHome

If sChkHome = "on" then
	sHome = "Y" 
else
	sHome = "N" 
end if

if sMoreThanOne = "on" then
	session("sMoreThanOne") = "Y"
else
	session("sMoreThanOne") = "N"
end if

sMoreThanOne = session("sMoreThanOne")

' -----------------------------------------------------------------------------------------------------------------------------



' -------------------------------------------------------- Main function ------------------------------------------------------

If sErrorMessage = "" then

		sErrorMessage = validateData()

dim sCheckFundType

if sFormType = "IRP3B" OR sFormType = "IRP3C" OR sFormType = "IRP3A" then
	sCheckFundType = "N"
else
	sCheckFundType = "Y"
end if
		
if sCheckFundType = "Y" then		
'------------------------------------------------------ Check if the fund can be used for this form ------
			
			dim dllValFundType
			dim sValFundType
			
				set dllValFundType = SERVER.CreateObject("KeyTax.clsFunds")
					sValFundType = dllValFundType.ValFundType(sFormType, nFundID)
				
				if sValFundType = "N" then
					sErrorMessage = "The fund that you have chosen, is not valid for this request."
				end if
			
			
'-----------------------------------------------------------------------------------------------------------------
end if 'sFormType <> "IRP3B"

		if sErrorMessage = "" then
			
			sErrorMessage = valDirective(nFundID)
			
		end if
	
end If 'sErrorMessage = "" AND len(nFundNumber) = 11

if sErrorMessage = "" then
	sErrorMessage = InsertTempInfo()
end if

if sErrorMessage = "" then
	sRedirectPage = "../forms/" & sFormType & ".asp"
	Response.Redirect (sRedirectPage)
end if
' -----------------------------------------------------------------------------------------------------------------------------




if sErrorMessage = "" then
' -------------------------------------------------------- Get Fund data from database ----------------------------------------

on error resume next

	Dim dllFundDetails
	dim sFundDetails
	
	Dim sFundType
	Dim sFundTypeMeaning
	Dim sFundCreateRsnCode
	Dim sFundCreateRsnDesc
	Dim sFundName
	Dim sContactPerson
	Dim sTelCode
	Dim sTelNumber
	Dim sFundPost1
	Dim sFundPost2
	Dim sFundPost3
	Dim sFundPost4
	Dim sFundPostCode
	
	if nFundID <> "" then
		Set dllFundDetails = Server.CreateObject("KeyTax.clsFundDetail")
			sFundDetails = dllFundDetails.FundDetail(nFundID, nFundNumber, sFundType, sFundTypeMeaning, sFundCreateRsnCode, sFundCreateRsnDesc, sFundName, sContactPerson, sTelCode, sTelNumber, sFundPost1, sFundPost2, sFundPost3, sFundPost4, sFundPostCode)
			
		session("sOldFund") = "Yes"
	else
		nFundID = "null"
		session("sOldFund") = "No"
	end if 'nFundID <> ""
	
' -----------------------------------------------------------------------------------------------------------------------------





' -------------------------------------------------------- Get Employer data from database ------------------------------------
	
	'if loading a Form AD request or a Form IRP3b, make sure that the PAYE reference number is filled in.
	
	dim sGetEmployerDetail
	
	if sFormType = "FORM_AD" OR sFormType = "IRP3B" OR sFormType = "IRP3C" OR sFormType = "IRP3A" then
		sGetEmployerDetail = "Y"
	else
		sGetEmployerDetail = "N"
	end if
	
	if sGetEmployerDetail = "Y" then
	
		if nPAYERefNr = "" or nEmpID = "" then
		
			sErrorMessage = "A PAYE reference number must be supplied when loading this request."
		
		else
			
			dim dllEmpDetails
			dim sEmpDetails
			
			'employer details
			Dim sEmployer
			Dim sEmpContactPerson
			Dim sEmpTelCode
			Dim sEmpTelNumber
			Dim sEmpHome1
			Dim sEmpHome2
			Dim sEmpHome3
			Dim sEmpHome4
			Dim sEmpHomeCode
			Dim sEmpPost1
			Dim sEmpPost2
			Dim sEmpPost3
			Dim sEmpPost4
			Dim sEmpPostCode
			
			Set dllEmpDetails = Server.CreateObject("KeyTax.clsEmployerDetail")
				sEmpDetails = dllEmpDetails.GetEmployerDetail(nEmpID, nPAYERefNr, sEmployer, sEmpContactPerson, sEmpTelCode, sEmpTelNumber, sEmpHome1, sEmpHome2, sEmpHome3, sEmpHome4, sEmpHomeCode, sEmpPost1, sEmpPost2, sEmpPost3, sEmpPost4, sEmpPostCode)
				
				
			if sEmpDetails = "No details found" then
				sEmpDetails = "None"	
				sNewEmployer = "Yes"
			else
				sNewEmployer = "No"
			end if ' sEmpDetails = "No details found"
		end if
			
		
	end if 'sFormType = "FORM_AD" or sFormType = "IRP3B" 
' -----------------------------------------------------------------------------------------------------------------------------








' -------------------------------------------------------- validate the submitted data ----------------------------------------

Public Function validateData()
Dim nValidateIRN
Dim ValidateFormType
Dim sFormName

	If sFormType <> "IRP3B" Then
		If sNoTaxRefNoRsn = "" And nIncomeTaxReferenceNo = "" Then
			sErrorMessage = "Please specify either a reason why no income tax number exists or provide a number"
		End If
	End If
	
	If sFormType = "IRP3B" Then
		If nIncomeTaxReferenceNo = "" Then
			sErrorMessage = "Please specify an Income Tax Number"
		End If
	End If
	
	if nIncomeTaxReferenceNo <> "" AND NOT isNumeric(nIncomeTaxReferenceNo)then
		sErrorMessage = "The Income Tax Reference number can only contain numbers."
	end if
	
	if nIncomeTaxReferenceNo = "" and sNoTaxRefNoRsn = "0" then
		sErrorMessage = "Please enter EITHER the Income Tax Number or specify why no Income Tax Number is available."
	end if
	
	'if nIncomeTaxReferenceNo <> "" and sNoTaxRefNoRsn <> "0" then
	'	sErrorMessage = "Please enter EITHER the Income Tax Number or specify why no Income Tax Number is available."
	'end if
	
	if sNoTaxRefNoRsn = "99" AND sNoTaxRefNoText = "" Then
		sErrorMessage = "Please provide a reason why no income number was provided"
	end if 
	
	if nRSAID <> "" AND NOT isNumeric(nRSAID) then
		sErrorMessage = "The RSA ID number can only contain numbers."
	end if
	
	if not isNumeric(dtBirthDateFull) then
		sErrorMessage = "The Date of Birth can only contain numbers."
	end if
	
	if not isNumeric(dtBirthDate) then
		sErrorMessage = "The Date of Birth can only contain numbers."
	end if
		
	if sFormType = "FORM_AD" or sFormType = "IRP3A" or sFormType = "IRP3B" or sFormType = "IRP3C" Then
		If nPAYERefNr = "" then
			sErrorMessage = "A PAYE reference number must be supplied when loading this request."
		End If
	end if
	
	if nPAYERefNr <> "" AND NOT isNumeric(nPAYERefNr) then
		sErrorMessage = "The PAYE Reference number can only contain numbers."
	end if
	
	If nPAYERefNr <> "" and nEmpID = "" Then
		ValidatePAYE(nPAYERefNr)
	End If
	
	if nIncomeTaxReferenceNo <> "" then
		nValidateIRN = cdbl(nIncomeTaxReferenceNo)
		nValidateIRN = Left(nIncomeTaxReferenceNo, 1)
	end if

	Select Case sFormType
		Case "FORM_AD"
			ValidateFormType = "Individual"
			sFormName = "Form A&D"
		Case "FORM_B"
			ValidateFormType = "Individual"
			sFormName = "Form B"
		Case "FORM_C"
			ValidateFormType = "Individual"
			sFormName = "Form C"
		Case "IRP3A"
			ValidateFormType = "Individual"
			sFormName = "IRP3A"
		'Case "IRP3B"
		'	ValidateFormType = "Company"
		'	sFormName = "IRP3B"
		'Case "IRP3C"
		'	ValidateFormType = "Company"
		'	sFormName = "IRP3C"
	End Select
	If nValidateIRN <> "" Then
		If ValidateFormType = "Individual" Then
			If CDbl(nValidateIRN) > 3 Then
				sErrorMessage = "A " & sFormName & " may only be submitted for individual income tax numbers (These numbers start with 0 / 1 / 2 / 3)"
			End If
		ElseIf ValidateFormType = "Company" Then
			If CDbl(nValidateIRN) <> 9 Then
				sErrorMessage = "A " & sFormName & " may only be submitted for company income tax numbers (These numbers start with a 9)"
			End If
		End If
	End If
	
	If sFundType = "Retirement annuity fund" and sFormName = "Form B" Then
		sErrorMessage = "Cannot withraw from a Retirement annuity fund when using a FormB"
	End If
	
	validateData = sErrorMessage

End Function

' -----------------------------------------------------------------------------------------------------------------------------



'shane:delete code so that birthdate can be returned in proper format ie yyyymmdd

' -------------------------------------------------------- change date into correct format ------------------------------------
'if dtBirthDate <> "" then
'	Set dllDateConvert = Server.CreateObject("Keytax.clsDateConvert")
'		dtBirthDate = dllDateConvert.StringDateToLongDate(dtBirthDate)
'end if 'dtBirthDate <> ""
' -----------------------------------------------------------------------------------------------------------------------------






' -------------------------------------------------------- validate directive data --------------------------------------------

Public Function valDirective(nFundID)
	dim valDirectiveData
	
	set valDirectiveData = Server.CreateObject("KeyTax.clsValidations")	
	
	valDirective = valDirectiveData.ValidateFrontPage(nFundID, session("KeyTaxUser"), nFundNumber, sNoTaxRefNoRsn, sNoTaxRefNoText, nIncomeTaxReferenceNo, nRSAID, sOtherID, sSurname, sInitials, sFirstNames, dtBirthDate, sChkHome, sPost1, sPost2, sPost3, sPost4, sPostCode, sHome1, sHome2, sHome3, sHome4, sHomeCode, dtBirthDateYear, dtBirthDateMonth, dtBirthDateDay, sEmailTo)
	
	If valDirective = "" AND session("sOldFund") = "Yes" then
		valDirective = valDirectiveData.Val_FormType_FundNr(sFormType, nFundID, session("KeyTaxUser"))
	end If
	
End Function
' -----------------------------------------------------------------------------------------------------------------------------


'Response.Write "<br>Just after val directive data - " & nPAYERefNr


' -------------------------------------------------------- write all values to the temptable ----------------------------------
Public Function InsertTempInfo()
	
	dim dllTempTableInsert
	dim sTempTableInsert
	dim nTEMPSeqNum
	
	Set dllTempTableInsert = Server.CreateObject("KeyTax.clsTempTableInsert")
		
	'Set dllDateConvert = Server.CreateObject("KeyTax.clsDateConvert")
		
		'dtBirthDateFull = dtBirthDateYear & dtBirthDateMonth & dtBirthDateDay
		
				
		nComID					= cstr(nComID)
		nUserID					= cstr(nUserID)
		sMoreThanOne			= cstr(sMoreThanOne)
		sEmailTo				= cstr(sEmailTo)
		nIncomeTaxReferenceNo	= cstr(nIncomeTaxReferenceNo)
		sNoTaxRefNoRsn			= cstr(sNoTaxRefNoRsn)
		sNoTaxRefNoText			= cstr(sNoTaxRefNoText)
		nRSAID					= cstr(nRSAID)
		sOtherID				= cstr(sOtherID)
		sSurname				= cstr(sSurname)
		sFirstNames				= cstr(sFirstNames)
		sInitials				= cstr(sInitials)
		dtBirthDate				= cstr(dtBirthDate)
		sHome1					= cstr(sHome1)
		sHome2					= cstr(sHome2)
		sHome3					= cstr(sHome3)
		sHome4					= cstr(sHome4)
		sHomeCode				= cstr(sHomeCode)
		sPost1					= cstr(sPost1)
		sPost2					= cstr(sPost2)
		sPost3					= cstr(sPost3)
		sPost4					= cstr(sPost4)
		sPostCode				= cstr(sPostCode)
		sFormType				= cstr(sFormType)
		nFundID					= cstr(nFundID)
		nEmpID					= cstr(nEmpID)

		dim nTEMPSeqNumValues
		
		'Response.write "" & nComID & ", " & nUserID & ", " & sMoreThanOne & ", " & sEmailTo & ", " & nIncomeTaxReferenceNo & ", " & sNoTaxRefNoRsn & ", " & sNoTaxRefNoText & ", " & nRSAID & ", " & sOtherID & ", " & sSurname & ", " & sFirstNames & ", " & sInitials & ", " & dtBirthDateFull & ", " & sHome1 & ", " & sHome2 & ", " & sHome3 & ", " & sHome4 & ", " & sHomeCode & ", " & sPost1 & ", " & sPost2 & ", " & sPost3 & ", " & sPost4 & ", " & sPostCode & ", " & sFormType & ", " & nFundID & ", " & nEmpID
		'Response.end
		nTEMPSeqNum = dllTempTableInsert.InsertTempInfo(nComID, nUserID, sMoreThanOne, sEmailTo, nIncomeTaxReferenceNo, sNoTaxRefNoRsn, sNoTaxRefNoText, nRSAID, sOtherID, sSurname, sFirstNames, sInitials, dtBirthDate, sHome1, sHome2, sHome3, sHome4, sHomeCode, sPost1, sPost2, sPost3, sPost4, sPostCode, sFormType, nFundID, nEmpID)
		
	session("nTEMPSeqNum") = nTEMPSeqNum
	
End Function
' -----------------------------------------------------------------------------------------------------------------------------

end if 'sErrorMessage = ""


'Shane: New code added. When user captures PAYE ref no last and sibmits form, validation is first done
'to check if the emp id field has a value, before submitting the form 05/03/2002
dim rsEmpDetails
dim objGetID
dim nNumberOfRecords
dim nNumberOfRecordsFromRS

Public Function ValidatePAYE(nPAYERefNr)

if nPAYERefNr <> "" then

	Set objGetID = Server.CreateObject("KeyTax.clsEmployers")
	
	
	set rsEmpDetails = objGetID.GetEmpIDRS(session("KeyTaxUser"), nPAYERefNr, session("nComID"))

	
	nNumberOfRecordsFromRS = rsEmpDetails.Recordcount
	
	if nNumberOfRecordsFromRS = 1 then 
		nEmpID = rsEmpDetails("emp_id").value
	else
		sErrorMessage = "The PAYE reference number does not exist. Please click on the Add New button to add a new employer" 
		nNumberOfRecords =  nNumberOfRecordsFromRS
	end if
	'cleanup
	set objGetID = nothing
	rsEmpDetails.close
	set rsEmpDetails = nothing
	
end if 'nPAYERefNr <> ""

End Function

If nNumberOfRecords = "" Then
	nNumberOfRecords = 0
End If

%>